import common from "../common";
import utils from "@common/utils/utils";
import constants from "../constants";

const sysResourceApi = {
    transferZTreeData (datas, filterBtn) {
        let list = [];
        datas.forEach(item => {
            let obj = {};
            obj.id = item.sid;
            obj.pId = item.parent_sid;
            obj.name = item.name;
            obj.res_type = item.res_type;
            if (utils.isNotEmpty(item.icon_url) && item.icon_url.indexOf('fa') !== - 1) {
                obj.iconSkin = item.icon_url;
                obj.iconColor = item.icon_color;
            }
            if (filterBtn) {
                if (utils.isNotEmpty(item.res_type) && ['button', 'right'].indexOf(item.res_type) === -1) {
                    list.push(obj);
                }
            } else {
                list.push(obj);
            }
            if (item.childrens && item.childrens.length > 0) {
                let childs = sysResourceApi.transferZTreeData(item.childrens, filterBtn);
                childs.forEach(item => {
                    list.push(item);
                });
            }
        });
        return list;
    },
    transferRoleZTreeData (datas, filterBtn) {
        let list = [];
        datas.forEach(item => {
            let obj = {};
            obj.id = item.sid;
            obj.p_id = item.parent_sid;
            obj.label = item.name;
            obj.res_type = item.res_type;
            if (utils.isNotEmpty(item.icon_url) && item.icon_url.indexOf('fa') !== - 1) {
                obj.iconSkin = item.icon_url;
                obj.iconColor = item.icon_color;
            }
            if (filterBtn) {
                if (utils.isNotEmpty(item.res_type) && item.res_type !== 'button') {
                    list.push(obj);
                }
            } else {
                list.push(obj);
            }
            if (item.childrens && item.childrens.length > 0) {
                obj.children = sysResourceApi.transferRoleZTreeData(item.childrens, filterBtn);
            }
        });
        return list;
    },
    findResourceTree4User: (data, success, error) => {
        return common.requestData(constants.API_FindResourceTree4User, data, success, error);
    },
    findSysResourceTree: (data, success, error) => {
        return common.requestData(constants.API_FindSysResourceTree, data, success, error);
    },
    findSysResourceTreeByRole: (data, success, error) => {
        return common.requestData(constants.API_FindSysResourceTreeByRole, data, success, error);
    },
    findSysResources: (data, success, error) => {
        return common.requestData(constants.API_FindSysResources, data, success, error);
    },
    saveSysResource: (data, success, error) => {
        return common.requestData(constants.API_SaveSysResource, data, success, error);
    },
    deleteSysResource: (data, success, error) => {
        return common.requestData(constants.API_DeleteSysResource, data, success, error);
    },
    changeResourceParentId: (data, success, error) => {
        return common.requestData(constants.API_ChangeResourceParentId, data, success, error);
    },
    findResource4RoleIds: (data, success, error) => {
        return common.requestData(constants.API_FindResource4RoleIds, data, success, error);
    },
};

export default sysResourceApi;
